package com.brooks.demo;

/**
 * @author Maoyan
 * @data 2020/10/18 15:28
 * @Version 1.0.0
 */
public class TestStop implements Runnable {
    private boolean flag = true;
    @Override
    public void run() {
        int i = 0;
        while (flag){
            System.out.println("run--Thread:"+(++i));
        }
    }

    //控制线程停止
    public void stop() {
        flag = false;
    }

    public static void main(String[] args) {
        TestStop testStop = new TestStop();
        new Thread(testStop).start();

        for (int i = 0; i < 1000; i++) {
            System.out.println("主线程："+i);
            if (i==900){
                testStop.stop();
                System.out.println("线程已经停止");
            }
        }
    }
}
